home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d892.lha / Indent / source / source.lha / indent.c < prev    next >
C/C++ Source or Header  |  1993-06-24  |  55KB  |  1,992 lines

  1. /* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.
  2.  
  3.    Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  4.    of the University of California. Copyright (c) 1976 Board of Trustees of
  5.    the University of Illinois. All rights reserved.
  6.  
  7.    Redistribution and use in source and binary forms are permitted
  8.    provided that
  9.    the above copyright notice and this paragraph are duplicated in all such
  10.    forms and that any documentation, advertising materials, and other
  11.    materials related to such distribution and use acknowledge that the
  12.    software was developed by the University of California, Berkeley, the
  13.    University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  14.    either University or Sun Microsystems may not be used to endorse or
  15.    promote products derived from this software without specific prior written
  16.    permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  18.    OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  19.  
  20. #include "sys.h"
  21. #include "indent.h"
  22. #include <ctype.h>
  23.  
  24. #ifdef AMIGA
  25. #include <exec/types.h>
  26. #include <dos/dos.h>
  27. #include <dos/dosextens.h>
  28. #include <dos/dosasl.h>
  29. #include <proto/dos.h>
  30.  
  31. #define MAXPATH 200
  32. #define MAXARGS 256
  33.  
  34. extern struct DosLibrary *DOSBase;
  35.  
  36. int expand_args ();
  37. #endif /* AMIGA */
  38.  
  39. void
  40. usage ()
  41. {
  42.   fprintf (stderr, "usage: indent file [-o outfile ] [ options ]\n");
  43.   fprintf (stderr, "       indent file1 file2 ... fileN [ options ]\n");
  44.   exit (1);
  45. }
  46.  
  47.  
  48. /* Stuff that needs to be shared with the rest of indent.
  49.    Documented in indent.h.  */
  50. char *labbuf;
  51. char *s_lab;
  52. char *e_lab;
  53. char *l_lab;
  54. char *codebuf;
  55. char *s_code;
  56. char *e_code;
  57. char *l_code;
  58. char *combuf;
  59. char *s_com;
  60. char *e_com;
  61. char *l_com;
  62. struct buf save_com;
  63. char *bp_save;
  64. char *be_save;
  65. int code_lines;
  66. int line_no;
  67. struct fstate keywordf;
  68. struct fstate stringf;
  69. struct fstate boxcomf;
  70. struct fstate blkcomf;
  71. struct fstate scomf;
  72. struct fstate bodyf;
  73. int break_comma;
  74.  
  75. /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.
  76.       Note:  This may change bufstruc.ptr.  */
  77. #define need_chars(bufstruc, req) \
  78.   if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \
  79. {\
  80.          int cur_chars = bufstruc.end - bufstruc.ptr;\
  81.          bufstruc.size *= 2;\
  82.          bufstruc.ptr = xrealloc (bufstruc.ptr,bufstruc.size);\
  83.          bufstruc.end = bufstruc.ptr + cur_chars;\
  84. }
  85.  
  86. /* True if there is an embedded comment on this code line */
  87. int embedded_comment_on_line;
  88.  
  89. int else_or_endif;
  90.  
  91. /* structure indentation levels */
  92. int *di_stack;
  93.  
  94. /* Currently allocated size of di_stack.  */
  95. int di_stack_alloc;
  96.  
  97. /* when this is positive, we have seen a ? without
  98.    the matching : in a <c>?<s>:<s> construct */
  99. int squest;
  100.  
  101. #define CHECK_CODE_SIZE \
  102.     if (e_code >= l_code) { \
  103.         register nsize = l_code-s_code+400; \
  104.         codebuf = (char *) xrealloc (codebuf, nsize); \
  105.         e_code = codebuf + (e_code-s_code) + 1; \
  106.         l_code = codebuf + nsize - 5; \
  107.         s_code = codebuf + 1; \
  108.     }
  109.  
  110. #define CHECK_LAB_SIZE \
  111.     if (e_lab >= l_lab) { \
  112.         register nsize = l_lab-s_lab+400; \
  113.         labbuf = (char *) xrealloc (labbuf, nsize); \
  114.         e_lab = labbuf + (e_lab-s_lab) + 1; \
  115.         l_lab = labbuf + nsize - 5; \
  116.         s_lab = labbuf + 1; \
  117.     }
  118.  
  119. static void
  120. indent (this_file)
  121.      struct file_buffer *this_file;
  122. {
  123.   register int i;
  124.   enum codes hd_type;
  125.   register char *t_ptr;
  126.   enum codes type_code;
  127.  
  128.   /* current indentation for declarations */
  129.   int dec_ind = 0;
  130.  
  131.   /* used when buffering up comments to remember that
  132.      a newline was passed over */
  133.   int flushed_nl = 0;        
  134.   int force_nl = 0;
  135.  
  136.   /* true when we've just see a case; determines what to do
  137.      with the following colon */
  138.   int scase = 0;
  139.  
  140.   /* true when in the expression part of if(...), while(...), etc. */
  141.   int sp_sw = 0;
  142.  
  143.   /* True if we have just encountered the end of an if (...), etc. (i.e. the
  144.      ')' of the if (...) was the last token).  The variable is set to 2 in
  145.      the middle of the main token reading loop and is decremented at the
  146.      beginning of the loop, so it will reach zero when the second token after
  147.      the ')' is read.  */
  148.   int last_token_ends_sp = 0;
  149.  
  150.   /* true iff last keyword was an else */
  151.   int last_else = 0;
  152.  
  153.  
  154.   in_prog = in_prog_pos = this_file->data;
  155.   in_prog_size = this_file->size;
  156.  
  157.   hd_type = code_eof;
  158.   dec_ind = 0;
  159.   last_token_ends_sp = false;
  160.   last_else = false;
  161.   sp_sw = force_nl = false;
  162.   scase = false;
  163.   squest = false;
  164.  
  165. #if 0
  166.   if (com_ind <= 1)
  167.     com_ind = 2;        /* dont put normal comments before column 2 */
  168. #endif
  169.  
  170.   if (troff)
  171.     {
  172.       if (bodyf.font[0] == 0)
  173.     parsefont (&bodyf, "R");
  174.       if (scomf.font[0] == 0)
  175.     parsefont (&scomf, "I");
  176.       if (blkcomf.font[0] == 0)
  177.     blkcomf = scomf, blkcomf.size += 2;
  178.       if (boxcomf.font[0] == 0)
  179.     boxcomf = blkcomf;
  180.       if (stringf.font[0] == 0)
  181.     parsefont (&stringf, "L");
  182.       if (keywordf.font[0] == 0)
  183.     parsefont (&keywordf, "B");
  184.       writefdef (&bodyf, 'B');
  185.       writefdef (&scomf, 'C');
  186.       writefdef (&blkcomf, 'L');
  187.       writefdef (&boxcomf, 'X');
  188.       writefdef (&stringf, 'S');
  189.       writefdef (&keywordf, 'K');
  190.     }
  191.   if (block_comment_max_col <= 0)
  192.     block_comment_max_col = max_col;
  193.   if (decl_com_ind <= 0)    /* if not specified by user, set this */
  194.     decl_com_ind =
  195.       ljust_decl ? (com_ind <= 10 ? 2 : com_ind - 8) : com_ind;
  196.   if (continuation_indent == 0)
  197.     continuation_indent = ind_size;
  198.   fill_buffer ();        /* get first batch of stuff into input buffer */
  199.  
  200. #if 0
  201.   parse (semicolon);
  202. #endif
  203.  
  204.   {
  205.     register char *p = buf_ptr;
  206.     register col = 1;
  207.  
  208.     while (1)
  209.       {
  210.     if (*p == ' ')
  211.       col++;
  212.     else if (*p == TAB)
  213.       col = tabsize - (col % tabsize) + 1;
  214.     else if (*p == EOL)
  215.       col = 1;
  216.     else
  217.       break;
  218.  
  219.     p++;
  220.       }
  221.  
  222. #if 0
  223.     if (col > ind_size)
  224.       parser_state_tos->ind_level = parser_state_tos->i_l_follow = col;
  225. #endif
  226.   }
  227.  
  228.   if (troff)
  229.     {
  230.       register char *p = in_name, *beg = in_name;
  231.  
  232.       while (*p)
  233.     if (*p++ == '/')
  234.       beg = p;
  235.       fprintf (output, ".Fn \"%s\"\n", beg);
  236.     }
  237.   /* START OF MAIN LOOP */
  238.  
  239.   while (1)
  240.     {                /* this is the main loop.  it will go until
  241.                    we reach eof */
  242.       int is_procname;
  243.  
  244.       type_code = lexi ();    /* lexi reads one token.  "token" points to
  245.                    the actual characters. lexi returns a code
  246.                    indicating the type of token */
  247.  
  248.       if (last_token_ends_sp > 0)
  249.     last_token_ends_sp--;
  250.       is_procname = parser_state_tos->procname[0];
  251.  
  252.       /* The following code moves everything following an if (), while (),
  253.          else, etc. up to the start of the following stmt to a buffer. This
  254.          allows proper handling of both kinds of brace placement. */
  255.  
  256.       flushed_nl = false;
  257.       while (parser_state_tos->search_brace)
  258.     {
  259.       /* After scanning an if(), while (), etc., it might be necessary to
  260.          keep track of the text between the if() and the start of the
  261.          statement which follows.  Use save_com to do so.  */
  262.  
  263.       switch (type_code)
  264.         {
  265.         case newline:
  266.           ++line_no;
  267.           flushed_nl = true;
  268.         case form_feed:
  269.           break;        /* form feeds and newlines found here will be
  270.                    ignored */
  271.  
  272.         case lbrace:    /* this is a brace that starts the compound
  273.                    stmt */
  274.           if (save_com.end == save_com.ptr)
  275.         {
  276.           /* ignore buffering if a comment wasnt stored up */
  277.           parser_state_tos->search_brace = false;
  278.           goto check_type;
  279.         }
  280.           /* We need to put the '{' back into save_com somewhere.  */
  281.           if (btype_2)
  282.         /* Put it at the beginning, e.g. if (foo) { / * comment here *
  283.            / */
  284.  
  285.         save_com.ptr[0] = '{';
  286.  
  287.           else
  288.         {
  289.           /* Put it at the end, e.g. if (foo) / * comment here * / { */
  290.  
  291.           /* Putting in this newline causes a dump_line to occur
  292.              right after the comment, thus insuring that it will be
  293.              put in the correct column.  */
  294.           *save_com.end++ = EOL;
  295.           *save_com.end++ = '{';
  296.         }
  297.  
  298.           /* Go to common code to get out of this loop.  */
  299.           goto sw_buffer;
  300.  
  301.         case comment:    /* we have a comment, so we must copy it into
  302.                    the buffer */
  303.           if (!flushed_nl || save_com.end != save_com.ptr)
  304.         {
  305.           need_chars (save_com, 10);
  306.           if (save_com.end == save_com.ptr)
  307.             {        /* if this is the first comment, we must set
  308.                    up the buffer */
  309.               save_com.ptr[0] = save_com.ptr[1] = ' ';
  310.               save_com.end = save_com.ptr + 2;
  311.             }
  312.           else
  313.             {
  314.               *save_com.end++ = EOL;    /* add newline between
  315.                            comments */
  316.               *save_com.end++ = ' ';
  317.               --line_no;
  318.             }
  319.           *save_com.end++ = '/';    /* copy in start of comment */
  320.           *save_com.end++ = '*';
  321.  
  322.           for (;;)
  323.             {        /* loop until we get to the end of the
  324.                    comment */
  325.               /* make sure there is room for this character and
  326.                  (while we're at it) the '/' we might add at the end
  327.                  of the loop. */
  328.               need_chars (save_com, 2);
  329.               *save_com.end = *buf_ptr++;
  330.               if (buf_ptr >= buf_end)
  331.             {
  332.               fill_buffer ();
  333.               if (had_eof)
  334.                 {
  335.                   diag (1, "Unclosed comment", 0, 0);
  336.                   exit (1);
  337.                 }
  338.             }
  339.  
  340.               if (*save_com.end++ == '*' && *buf_ptr == '/')
  341.             break;    /* we are at end of comment */
  342.  
  343.             }
  344.           *save_com.end++ = '/';    /* add ending slash */
  345.           if (++buf_ptr >= buf_end)    /* get past / in buffer */
  346.             fill_buffer ();
  347.           break;
  348.         }
  349.         default:        /* it is the start of a normal statment */
  350.           if (flushed_nl)    /* if we flushed a newline, make sure it is
  351.                    put back */
  352.         force_nl = true;
  353.           if ((type_code == sp_paren && *token == 'i'
  354.            && last_else && else_if)
  355.           ||
  356.           (type_code == sp_nparen && *token == 'e'
  357.            && e_code != s_code && e_code[-1] == '}'))
  358.         force_nl = false;
  359.  
  360.           if (save_com.end == save_com.ptr)
  361.         {
  362.           /* ignore buffering if comment wasnt saved up */
  363.           parser_state_tos->search_brace = false;
  364.           goto check_type;
  365.         }
  366.           if (force_nl)
  367.         {        /* if we should insert a nl here, put it into
  368.                    the buffer */
  369.           force_nl = false;
  370.           --line_no;    /* this will be re-increased when the nl is
  371.                    read from the buffer */
  372.           need_chars (save_com, 2);
  373.           *save_com.end++ = EOL;
  374.           *save_com.end++ = ' ';
  375.           if (verbose && !flushed_nl)    /* print error msg if the
  376.                            line was not already
  377.                            broken */
  378.             diag (0, "Line broken", 0, 0);
  379.           flushed_nl = false;
  380.         }
  381.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  382.         {
  383.           need_chars (save_com, 1);
  384.           *save_com.end++ = *t_ptr;    /* copy token into temp
  385.                            buffer */
  386.         }
  387.           parser_state_tos->procname = "\0";
  388.  
  389.         sw_buffer:
  390.           parser_state_tos->search_brace = false;    /* stop looking for
  391.                                start of stmt */
  392.           bp_save = buf_ptr;/* save current input buffer */
  393.           be_save = buf_end;
  394.           buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  395.                        lexi will take tokens out of
  396.                        save_com */
  397.           need_chars (save_com, 1);
  398.           *save_com.end++ = ' ';    /* add trailing blank, just in case */
  399.           buf_end = save_com.end;
  400.           save_com.end = save_com.ptr;    /* make save_com empty */
  401.           break;
  402.         }            /* end of switch */
  403.       /* we must make this check, just in case there was an unexpected
  404.          EOF */
  405.       if (type_code != code_eof)
  406.         type_code = lexi ();/* read another token */
  407.       /* if (parser_state_tos->search_brace)
  408.          parser_state_tos->procname[0] = 0; */
  409.       if ((is_procname = parser_state_tos->procname[0]) && flushed_nl
  410.           && !procnames_start_line && parser_state_tos->in_decl
  411.           && type_code == ident)
  412.         flushed_nl = 0;
  413.     }            /* end of while (search_brace) */
  414.       last_else = 0;
  415.  
  416.     check_type:
  417.       if (type_code == code_eof)
  418.     {            /* we got eof */
  419.       if (s_lab != e_lab || s_code != e_code
  420.           || s_com != e_com)/* must dump end of line */
  421.         dump_line ();
  422.       if (parser_state_tos->tos > 1)    /* check for balanced braces */
  423.         diag (1, "Stuff missing from end of file.", 0, 0);
  424.  
  425.       if (verbose)
  426.         {
  427.           fprintf (stderr, "There were %d output lines and %d comments\n",
  428.               (int) out_lines, (int) out_coms);
  429.           fprintf (stderr, "(Lines with comments)/(Lines with code): %6.3f\n",
  430.               (1.0 * com_lines) / (code_lines > 0 ? code_lines : 1) );
  431.         }
  432.       fflush (output);
  433.       if (found_err)
  434.         exit (found_err);
  435.  
  436.       return;
  437.     }
  438.  
  439.       if ((type_code != comment) &&
  440.       (type_code != cplus_comment) &&
  441.       (type_code != newline) &&
  442.       (type_code != preesc) &&
  443.       (type_code != form_feed))
  444.     {
  445.       if (force_nl &&
  446.           (type_code != semicolon) &&
  447.           (type_code != lbrace || !btype_2))
  448.         {
  449.           /* we should force a broken line here */
  450.           if (verbose && !flushed_nl)
  451.         diag (0, "Line broken", 0, 0);
  452.           flushed_nl = false;
  453.           dump_line ();
  454.           parser_state_tos->want_blank = false;    /* dont insert blank at
  455.                                line start */
  456.           force_nl = false;
  457.         }
  458.       parser_state_tos->in_stmt = true;    /* turn on flag which causes
  459.                            an extra level of
  460.                            indentation. this is
  461.                            turned off by a ; or } */
  462.       if (s_com != e_com)
  463.         {            /* the turkey has embedded a comment in a
  464.                    line. Move it from the com buffer to the
  465.                    code buffer.  */
  466.           /* Do not add a space before the comment if it is the first
  467.              thing on the line.  */
  468.           if (e_code != s_code)
  469.         {
  470.           *e_code++ = ' ';
  471.           embedded_comment_on_line = 2;
  472.         }
  473.           else
  474.         embedded_comment_on_line = 1;
  475.  
  476.           for (t_ptr = s_com; *t_ptr; ++t_ptr)
  477.         {
  478.           CHECK_CODE_SIZE;
  479.           *e_code++ = *t_ptr;
  480.         }
  481.           *e_code++ = ' ';
  482.           *e_code = '\0';    /* null terminate code sect */
  483.           parser_state_tos->want_blank = false;
  484.           e_com = s_com;
  485.         }
  486.     }
  487.       else if (type_code != comment
  488.            && type_code != cplus_comment)
  489.     /* preserve force_nl thru a comment but
  490.        cancel forced newline after newline, form feed, etc */
  491.        force_nl = false;
  492.  
  493.  
  494.  
  495.       /*-----------------------------------------------------*\
  496.       |       do switch on type of token scanned              |
  497.       \*-----------------------------------------------------*/
  498.       CHECK_CODE_SIZE;
  499.       switch (type_code)
  500.     {            /* now, decide what to do with the token */
  501.  
  502.     case form_feed:    /* found a form feed in line */
  503.       parser_state_tos->use_ff = true;    /* a form feed is treated
  504.                            much like a newline */
  505.       dump_line ();
  506.       parser_state_tos->want_blank = false;
  507.       break;
  508.  
  509.     case newline:
  510.       if ((parser_state_tos->last_token != comma
  511.            || !leave_comma || !break_comma
  512.            || parser_state_tos->p_l_follow > 0
  513.            || parser_state_tos->block_init
  514.            || s_com != e_com)
  515.           && (parser_state_tos->last_token != rbrace || !btype_2)
  516.           || (compute_code_target () + (e_code - s_code)
  517.           > max_col - tabsize))
  518.         {
  519.           dump_line ();
  520.           parser_state_tos->want_blank = false;
  521.         }
  522.       /* If we were on the line with a #else or a #endif, we aren't
  523.          anymore.  */
  524.       else_or_endif = false;
  525.       ++line_no;        /* keep track of input line number */
  526.       break;
  527.  
  528.     case lparen:
  529.       /* Braces in initializer lists should be put on new lines. This is
  530.          necessary so that -gnu does not cause things like char
  531.          *this_is_a_string_array[] = { "foo", "this_string_does_not_fit",
  532.          "nor_does_this_rather_long_string" } which is what happens
  533.          because we are trying to line the strings up with the
  534.          parentheses, and those that are too long are moved to the right
  535.          an ugly amount.
  536.     
  537.          However, if the current line is empty, the left brace is
  538.          already on a new line, so don't molest it.  */
  539.       if (token[0] == '{'
  540.           && (s_code != e_code || s_com != e_com || s_lab != e_lab))
  541.         {
  542.           dump_line ();
  543.           /* Do not put a space before the '{'.  */
  544.           parser_state_tos->want_blank = false;
  545.         }
  546.  
  547.       /* Count parens so we know how deep we are.  */
  548.       if (++parser_state_tos->p_l_follow
  549.           >= parser_state_tos->paren_indents_size)
  550.         {
  551.           parser_state_tos->paren_indents_size *= 2;
  552.           parser_state_tos->paren_indents = (short *)
  553.         xrealloc ((char *) parser_state_tos->paren_indents,
  554.               parser_state_tos->paren_indents_size
  555.               * sizeof (short));
  556.         }
  557.       parser_state_tos->paren_depth++;
  558.       if (parser_state_tos->want_blank && *token != '['
  559.           && (parser_state_tos->last_token != ident || proc_calls_space
  560.           || (parser_state_tos->its_a_keyword
  561.               && (!parser_state_tos->sizeof_keyword
  562.               || blank_after_sizeof))))
  563.         *e_code++ = ' ';
  564.  
  565.       if (parser_state_tos->in_decl && !parser_state_tos->block_init)
  566.         {
  567.           if (troff
  568.           && !parser_state_tos->dumped_decl_indent
  569.           && !is_procname
  570.           && parser_state_tos->last_token == decl)
  571.         {
  572.           parser_state_tos->dumped_decl_indent = 1;
  573.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  574.                (int) (dec_ind * 7),
  575.                token_end - token, token);
  576.           e_code += strlen (e_code);
  577.         }
  578.           else if (*token != '[')
  579.         {
  580.           while ((e_code - s_code) < dec_ind)
  581.             {
  582.               CHECK_CODE_SIZE;
  583.               *e_code++ = ' ';
  584.             }
  585.           *e_code++ = token[0];
  586.         }
  587.           else
  588.         *e_code++ = token[0];
  589.         }
  590.       else
  591.         *e_code++ = token[0];
  592.  
  593.       parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1]
  594.         = e_code - s_code;
  595.       if (sp_sw && parser_state_tos->p_l_follow == 1
  596.           && extra_expression_indent
  597.           && parser_state_tos->paren_indents[0] < 2 * ind_size)
  598.         parser_state_tos->paren_indents[0] = 2 * ind_size;
  599.       parser_state_tos->want_blank = false;
  600.  
  601.       if (parser_state_tos->in_or_st
  602.           && *token == '('
  603.           && parser_state_tos->tos <= 2)
  604.         {
  605.           /* this is a kluge to make sure that declarations will be
  606.              aligned right if proc decl has an explicit type on it, i.e.
  607.              "int a(x) {..." */
  608.           parse_lparen_in_decl ();
  609.  
  610.           /* Turn off flag for structure decl or initialization.  */
  611.           parser_state_tos->in_or_st = false;
  612.         }
  613.       if (parser_state_tos->sizeof_keyword)
  614.         parser_state_tos->sizeof_mask |= 1 << parser_state_tos->p_l_follow;
  615.  
  616.       /* The '(' that starts a cast can never be preceeded by an
  617.          indentifier or decl.  */
  618.       if (parser_state_tos->last_token == decl
  619.           || (parser_state_tos->last_token == ident
  620.           && parser_state_tos->last_rw != rw_return))
  621.         parser_state_tos->noncast_mask |=
  622.           1 << parser_state_tos->p_l_follow;
  623.       else
  624.         parser_state_tos->noncast_mask &=
  625.           ~(1 << parser_state_tos->p_l_follow);
  626.  
  627.       break;
  628.  
  629.     case rparen:
  630.       parser_state_tos->paren_depth--;
  631.       if (parser_state_tos->cast_mask
  632.           & (1 << parser_state_tos->p_l_follow)
  633.           & ~parser_state_tos->sizeof_mask)
  634.         {
  635.           parser_state_tos->last_u_d = true;
  636.           parser_state_tos->cast_mask &=
  637.         (1 << parser_state_tos->p_l_follow) - 1;
  638.           if (!parser_state_tos->cast_mask && cast_space)
  639.         parser_state_tos->want_blank = true;
  640.           else
  641.         parser_state_tos->want_blank = false;
  642.         }
  643.       else if (parser_state_tos->in_decl
  644.            && ! parser_state_tos->block_init
  645.            && parser_state_tos->paren_depth == 0)
  646.         parser_state_tos->want_blank = true;
  647.  
  648.       parser_state_tos->sizeof_mask
  649.         &= (1 << parser_state_tos->p_l_follow) - 1;
  650.       if (--parser_state_tos->p_l_follow < 0)
  651.         {
  652.           parser_state_tos->p_l_follow = 0;
  653.           diag (0, "Extra %c", (int) *token, 0);
  654.         }
  655.  
  656.       /* if the paren starts the line, then indent it */
  657.       if (e_code == s_code)
  658.         {
  659.           int level = parser_state_tos->p_l_follow;
  660.           parser_state_tos->paren_level = level;
  661.           if (level > 0)
  662.         paren_target = -parser_state_tos->paren_indents[level - 1];
  663.           else
  664.         paren_target = 0;
  665.         }
  666.       *e_code++ = token[0];
  667.  
  668. #if 0
  669.       if (!parser_state_tos->cast_mask || cast_space)
  670.         parser_state_tos->want_blank = true;
  671. #endif
  672.  
  673.       /* check for end of if (...), or some such */
  674.       if (sp_sw && (parser_state_tos->p_l_follow == 0))
  675.         {
  676.  
  677.           /* Indicate that we have just left the parenthesized expression
  678.              of a while, if, or for, unless we are getting out of the
  679.              parenthesized expression of the while of a do-while loop.
  680.              (do-while is different because a semicolon immediately
  681.              following this will not indicate a null loop body).  */
  682.           if (parser_state_tos->p_stack[parser_state_tos->tos]
  683.           != dohead)
  684.         last_token_ends_sp = 2;
  685.           sp_sw = false;
  686.           force_nl = true;    /* must force newline after if */
  687.           parser_state_tos->last_u_d = true;    /* inform lexi that a
  688.                                following operator is
  689.                                unary */
  690.           parser_state_tos->in_stmt = false;    /* dont use stmt
  691.                                continuation
  692.                                indentation */
  693.  
  694.           parse (hd_type);    /* let parser worry about if, or whatever */
  695.         }
  696.       parser_state_tos->search_brace = btype_2;    /* this should insure
  697.                                that constructs such
  698.                                as main(){...} and
  699.                                int[]{...} have their
  700.                                braces put in the
  701.                                right place */
  702.       break;
  703.  
  704.     case unary_op:        /* this could be any unary operation */
  705.       if (parser_state_tos->want_blank)
  706.         *e_code++ = ' ';
  707.  
  708.       if (troff
  709.           && !parser_state_tos->dumped_decl_indent
  710.           && parser_state_tos->in_decl && !is_procname)
  711.         {
  712.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  713.                (int) (dec_ind * 7),
  714.                token_end - token, token);
  715.           parser_state_tos->dumped_decl_indent = 1;
  716.           e_code += strlen (e_code);
  717.         }
  718.       else
  719.         {
  720.           char *res = token;
  721.           char *res_end = token_end;
  722.  
  723.           /* if this is a unary op in a declaration, we should
  724.          indent this token */
  725.           if (parser_state_tos->paren_depth == 0
  726.           && parser_state_tos->in_decl
  727.           && !parser_state_tos->block_init)
  728.         {
  729.           while ((e_code - s_code) < (dec_ind - (token_end - token)))
  730.             {
  731.               CHECK_CODE_SIZE;
  732.               *e_code++ = ' ';
  733.             }
  734.         }
  735.  
  736.           if (troff && token[0] == '-' && token[1] == '>')
  737.         {
  738.           static char resval[] = "\\(->";
  739.           res = resval;
  740.           res_end = res + sizeof (resval);
  741.         }
  742.  
  743.           for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  744.         {
  745.           CHECK_CODE_SIZE;
  746.           *e_code++ = *t_ptr;
  747.         }
  748.         }
  749.       parser_state_tos->want_blank = false;
  750.       break;
  751.  
  752.     case binary_op:    /* any binary operation */
  753.       if (parser_state_tos->want_blank
  754.           || (e_code > s_code && *e_code != ' '))
  755.         *e_code++ = ' ';
  756.  
  757.       {
  758.         char *res = token;
  759.         char *res_end = token_end;
  760. #define set_res(str) \
  761.           {\
  762.         static char resval[] = str;\
  763.         res = resval;\
  764.         res_end = res + sizeof(resval);\
  765.           }
  766.  
  767.         if (troff)
  768.           switch (token[0])
  769.         {
  770.         case '<':
  771.           if (token[1] == '=')
  772.             set_res ("\\(<=");
  773.           break;
  774.         case '>':
  775.           if (token[1] == '=')
  776.             set_res ("\\(>=");
  777.           break;
  778.         case '!':
  779.           if (token[1] == '=')
  780.             set_res ("\\(!=");
  781.           break;
  782.         case '|':
  783.           if (token[1] == '|')
  784.             {
  785.               set_res ("\\(br\\(br");
  786.             }
  787.           else if (token[1] == 0)
  788.             set_res ("\\(br");
  789.           break;
  790.         }
  791.  
  792.         for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  793.           {
  794.         CHECK_CODE_SIZE;
  795.         *e_code++ = *t_ptr;    /* move the operator */
  796.           }
  797.       }
  798.       parser_state_tos->want_blank = true;
  799.       break;
  800.  
  801.     case postop:        /* got a trailing ++ or -- */
  802.       *e_code++ = token[0];
  803.       *e_code++ = token[1];
  804.       parser_state_tos->want_blank = true;
  805.       break;
  806.  
  807.     case question:        /* got a ? */
  808.       squest++;        /* this will be used when a later colon
  809.                    appears so we can distinguish the
  810.                    <c>?<n>:<n> construct */
  811.       if (parser_state_tos->want_blank)
  812.         *e_code++ = ' ';
  813.       *e_code++ = '?';
  814.       parser_state_tos->want_blank = true;
  815.       break;
  816.  
  817.     case casestmt:        /* got word 'case' or 'default' */
  818.       scase = true;        /* so we can process the later colon
  819.                    properly */
  820.       goto copy_id;
  821.  
  822.     case colon:        /* got a ':' */
  823.       if (squest > 0)
  824.         {            /* it is part of the <c>?<n>: <n> construct */
  825.           --squest;
  826.           if (parser_state_tos->want_blank)
  827.         *e_code++ = ' ';
  828.           *e_code++ = ':';
  829.           parser_state_tos->want_blank = true;
  830.           break;
  831.         }
  832.       if (parser_state_tos->in_decl)
  833.         {
  834.           *e_code++ = ':';
  835.           parser_state_tos->want_blank = false;
  836.           break;
  837.         }
  838.       parser_state_tos->in_stmt = false;    /* seeing a label does not
  839.                            imply we are in a stmt */
  840.       for (t_ptr = s_code; *t_ptr; ++t_ptr)
  841.         *e_lab++ = *t_ptr;    /* turn everything so far into a label */
  842.       e_code = s_code;
  843.       *e_lab++ = ':';
  844.       *e_lab++ = ' ';
  845.       *e_lab = '\0';
  846.       /* parser_state_tos->pcas e will be used by dump_line to decide
  847.          how to indent the label. force_nl will force a case n: to be
  848.          on a line by itself */
  849.       force_nl = parser_state_tos->pcase = scase;
  850.       scase = false;
  851.       parser_state_tos->want_blank = false;
  852.       break;
  853.  
  854.     case semicolon:
  855.       /* we are not in an initialization or structure declaration */
  856.       parser_state_tos->in_or_st = false;
  857.       scase = false;
  858.       squest = 0;
  859.       /* The following code doesn't seem to do much good. Just because
  860.          we've found something like extern int foo();    or int (*foo)();
  861.          doesn't mean we are out of a declaration.  Now if it was serving
  862.          some purpose we'll have to address that.... if
  863.          (parser_state_tos->last_token == rparen)
  864.          parser_state_tos->in_parameter_declaration = 0; */
  865.       parser_state_tos->cast_mask = 0;
  866.       parser_state_tos->sizeof_mask = 0;
  867.       parser_state_tos->block_init = 0;
  868.       parser_state_tos->block_init_level = 0;
  869.       parser_state_tos->just_saw_decl--;
  870.  
  871.       if (parser_state_tos->in_decl
  872.           && s_code == e_code
  873.           && !parser_state_tos->block_init)
  874.         while ((e_code - s_code) < (dec_ind - 1))
  875.           {
  876.         CHECK_CODE_SIZE;
  877.         *e_code++ = ' ';
  878.           }
  879.  
  880.       /* if we were in a first level structure declaration,
  881.          we aren't any more */
  882.       parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0);
  883.  
  884. #if 0    /* Changed to allow "{}" inside parens, as when
  885.        passed to a macro.  -jla */
  886.       if ((!sp_sw || hd_type != forstmt)
  887.           && parser_state_tos->p_l_follow > 0)
  888.         {
  889.  
  890.           /* This should be true iff there were unbalanced parens in the
  891.              stmt.  It is a bit complicated, because the semicolon might
  892.              be in a for stmt */
  893.           diag (1, "Unbalanced parens", 0, 0);
  894.           parser_state_tos->p_l_follow = 0;
  895.           if (sp_sw)
  896.         {        /* this is a check for a if, while, etc. with
  897.                    unbalanced parens */
  898.           sp_sw = false;
  899.           parse (hd_type);    /* dont lose the if, or whatever */
  900.         }
  901.         }
  902. #endif
  903.  
  904.       /* If we have a semicolon following an if, while, or for, and the
  905.          user wants us to, we should insert a space (to show that there
  906.          is a null statement there).  */
  907.       if (last_token_ends_sp && space_sp_semicolon)
  908.         {
  909.           *e_code++ = ' ';
  910.         }
  911.       *e_code++ = ';';
  912.       parser_state_tos->want_blank = true;
  913.       /* we are no longer in the middle of a stmt */
  914.       parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0);
  915.  
  916.       if (!sp_sw)
  917.         {            /* if not if for (;;) */
  918.           parse (semicolon);/* let parser know about end of stmt */
  919.           force_nl = true;    /* force newline after a end of stmt */
  920.         }
  921.       break;
  922.  
  923.     case lbrace:        /* got a '{' */
  924.       parser_state_tos->in_stmt = false;    /* dont indent the {} */
  925.       if (!parser_state_tos->block_init)
  926.         force_nl = true;    /* force other stuff on same line as '{' onto
  927.                    new line */
  928.       else if (parser_state_tos->block_init_level <= 0)
  929.         parser_state_tos->block_init_level = 1;
  930.       else
  931.         parser_state_tos->block_init_level++;
  932.  
  933.       if (s_code != e_code && !parser_state_tos->block_init)
  934.         {
  935.           if (!btype_2)
  936.         {
  937.           dump_line ();
  938.           parser_state_tos->want_blank = false;
  939.         }
  940.           else
  941.         {
  942.           if (parser_state_tos->in_parameter_declaration
  943.               && !parser_state_tos->in_or_st)
  944.             {
  945.               parser_state_tos->i_l_follow = 0;
  946.               dump_line ();
  947.               parser_state_tos->want_blank = false;
  948.             }
  949.           else
  950.             parser_state_tos->want_blank = true;
  951.         }
  952.         }
  953.       if (parser_state_tos->in_parameter_declaration)
  954.         prefix_blankline_requested = 0;
  955.  
  956. #if 0    /* Changed to allow "{}" inside parens, as when
  957.        passed to a macro.  -jla */
  958.       if (parser_state_tos->p_l_follow > 0)
  959.         {            /* check for preceeding unbalanced parens */
  960.           diag (1, "Unbalanced parens", 0, 0);
  961.           parser_state_tos->p_l_follow = 0;
  962.           if (sp_sw)
  963.         {        /* check for unclosed if, for, etc. */
  964.           sp_sw = false;
  965.           parse (hd_type);
  966.           parser_state_tos->ind_level = parser_state_tos->i_l_follow;
  967.         }
  968.         }
  969. #endif
  970.       if (s_code == e_code)
  971.         parser_state_tos->ind_stmt = false;    /* dont put extra indentation
  972.                            on line with '{' */
  973.       if (parser_state_tos->in_decl && parser_state_tos->in_or_st)
  974.         {
  975.           /* This is a structure declaration.  */
  976.           if (parser_state_tos->dec_nest >= di_stack_alloc)
  977.         {
  978.           di_stack_alloc *= 2;
  979.           di_stack = (int *)
  980.             xrealloc ((char *) di_stack,
  981.                   di_stack_alloc * sizeof (*di_stack));
  982.         }
  983.           di_stack[parser_state_tos->dec_nest++] = dec_ind;
  984.           /* ?        dec_ind = 0; */
  985.         }
  986.       else
  987.         {
  988.           parser_state_tos->in_decl = false;
  989.           parser_state_tos->decl_on_line = false;    /* we cant be in the
  990.                                middle of a
  991.                                declaration, so dont
  992.                                do special
  993.                                indentation of
  994.                                comments */
  995.  
  996. #if 0                /* Doesn't work currently. */
  997.           if (blanklines_after_declarations_at_proctop
  998.           && parser_state_tos->in_parameter_declaration)
  999.         postfix_blankline_requested = 1;
  1000. #endif
  1001.           parser_state_tos->in_parameter_declaration = 0;
  1002.         }
  1003.       dec_ind = 0;
  1004.  
  1005.       /* We are no longer looking for an initializer or structure. Needed
  1006.          so that the '=' in "enum bar {a = 1" does not get interpreted as
  1007.          the start of an initializer.  */
  1008.       parser_state_tos->in_or_st = false;
  1009.  
  1010.       parse (lbrace);    /* let parser know about this */
  1011.       if (parser_state_tos->want_blank)    /* put a blank before '{' if
  1012.                            '{' is not at start of
  1013.                            line */
  1014.         *e_code++ = ' ';
  1015.       parser_state_tos->want_blank = false;
  1016.       *e_code++ = '{';
  1017.       parser_state_tos->just_saw_decl = 0;
  1018.       break;
  1019.  
  1020.     case rbrace:        /* got a '}' */
  1021.       /* semicolons can be omitted in declarations */
  1022.       if (parser_state_tos->p_stack[parser_state_tos->tos] == decl
  1023.           && !parser_state_tos->block_init)
  1024.         parse (semicolon);
  1025. #if 0    /* Changed to allow "{}" inside parens, as when
  1026.        passed to a macro.  -jla */
  1027.       if (parser_state_tos->p_l_follow)
  1028.         {            /* check for unclosed if, for, else. */
  1029.           diag (1, "Unbalanced parens", 0, 0);
  1030.           parser_state_tos->p_l_follow = 0;
  1031.           sp_sw = false;
  1032.         }
  1033. #endif
  1034.  
  1035.       parser_state_tos->just_saw_decl = 0;
  1036.       parser_state_tos->block_init_level--;
  1037.       if (s_code != e_code && !parser_state_tos->block_init)
  1038.         {            /* '}' must be first on line */
  1039.           if (verbose)
  1040.         diag (0, "Line broken", 0, 0);
  1041.           dump_line ();
  1042.         }
  1043.       *e_code++ = '}';
  1044.       parser_state_tos->want_blank = true;
  1045.       parser_state_tos->in_stmt = parser_state_tos->ind_stmt = false;
  1046.       if (parser_state_tos->dec_nest > 0)
  1047.         {            /* we are in multi-level structure
  1048.                    declaration */
  1049.           dec_ind = di_stack[--parser_state_tos->dec_nest];
  1050.           if (parser_state_tos->dec_nest == 0
  1051.           && !parser_state_tos->in_parameter_declaration)
  1052.         parser_state_tos->just_saw_decl = 2;
  1053.           parser_state_tos->in_decl = true;
  1054.         }
  1055.       prefix_blankline_requested = 0;
  1056.       parse (rbrace);    /* let parser know about this */
  1057.       parser_state_tos->search_brace
  1058.         = (cuddle_else
  1059.          && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead);
  1060.  
  1061.       if ((parser_state_tos->p_stack[parser_state_tos->tos] == stmtl
  1062.            && ((parser_state_tos->last_rw != rw_struct_like
  1063.             && parser_state_tos->last_rw != rw_decl)
  1064.            || ! btype_2))
  1065.           || (parser_state_tos->p_stack[parser_state_tos->tos] == ifhead)
  1066.           || (parser_state_tos->p_stack[parser_state_tos->tos] == dohead
  1067.           && !btype_2))
  1068.         force_nl = true;
  1069.       else if (parser_state_tos->tos <= 1
  1070.            && blanklines_after_procs
  1071.            && parser_state_tos->dec_nest <= 0)
  1072.         postfix_blankline_requested = 1;
  1073.  
  1074. #if 0
  1075.       parser_state_tos->search_brace
  1076.         = (cuddle_else
  1077.            && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead
  1078.            && (parser_state_tos->il[parser_state_tos->tos]
  1079.            >= parser_state_tos->ind_level));
  1080. #endif
  1081.  
  1082.       break;
  1083.  
  1084.     case swstmt:        /* got keyword "switch" */
  1085.       sp_sw = true;
  1086.       hd_type = swstmt;    /* keep this for when we have seen the
  1087.                    expression */
  1088.       parser_state_tos->in_decl = false;
  1089.       goto copy_id;        /* go move the token into buffer */
  1090.  
  1091.     case sp_paren:        /* token is if, while, for */
  1092.       sp_sw = true;        /* the interesting stuff is done after the
  1093.                    expression is scanned */
  1094.       hd_type = (*token == 'i' ? ifstmt :
  1095.              (*token == 'w' ? whilestmt : forstmt));
  1096.  
  1097.       /* remember the type of header for later use by parser */
  1098.       goto copy_id;        /* copy the token into line */
  1099.  
  1100.     case sp_nparen:    /* got else, do */
  1101.       parser_state_tos->in_stmt = false;
  1102.       if (*token == 'e')
  1103.         {
  1104.           if (e_code != s_code && (!cuddle_else || e_code[-1] != '}'))
  1105.         {
  1106.           if (verbose)
  1107.             diag (0, "Line broken", 0, 0);
  1108.           dump_line ();    /* make sure this starts a line */
  1109.           parser_state_tos->want_blank = false;
  1110.         }
  1111.           force_nl = true;    /* also, following stuff must go onto new
  1112.                    line */
  1113.           last_else = 1;
  1114.           parse (elselit);
  1115.         }
  1116.       else
  1117.         {
  1118.           if (e_code != s_code)
  1119.         {        /* make sure this starts a line */
  1120.           if (verbose)
  1121.             diag (0, "Line broken", 0, 0);
  1122.           dump_line ();
  1123.           parser_state_tos->want_blank = false;
  1124.         }
  1125.           force_nl = true;    /* also, following stuff must go onto new
  1126.                    line */
  1127.           last_else = 0;
  1128.           parse (dolit);
  1129.         }
  1130.       goto copy_id;        /* move the token into line */
  1131.  
  1132.     case decl:        /* we have a declaration type (int, register,
  1133.                    etc.) */
  1134.  
  1135.       if (! parser_state_tos->sizeof_mask)
  1136.         parse (decl);
  1137.  
  1138.       if (parser_state_tos->last_token == rparen
  1139.           && parser_state_tos->tos <= 1)
  1140.         {
  1141.           parser_state_tos->in_parameter_declaration = 1;
  1142.           if (s_code != e_code)
  1143.         {
  1144.           dump_line ();
  1145.           parser_state_tos->want_blank = false;
  1146.         }
  1147.         }
  1148.       if (parser_state_tos->in_parameter_declaration
  1149.           && indent_parameters
  1150.           && parser_state_tos->dec_nest == 0
  1151.           && parser_state_tos->p_l_follow == 0)
  1152.         {
  1153.           parser_state_tos->ind_level
  1154.         = parser_state_tos->i_l_follow = indent_parameters;
  1155.           parser_state_tos->ind_stmt = 0;
  1156.         }
  1157.  
  1158.       /* in_or_st set for struct or initialization decl. Don't set it if
  1159.          we're in ansi prototype */
  1160.       if (!parser_state_tos->paren_depth)
  1161.         parser_state_tos->in_or_st = true;
  1162.  
  1163.       parser_state_tos->in_decl = parser_state_tos->decl_on_line = true;
  1164. #if 0
  1165.       if (!parser_state_tos->in_or_st && parser_state_tos->dec_nest <= 0)
  1166. #endif
  1167.         if (parser_state_tos->dec_nest <= 0)
  1168.           parser_state_tos->just_saw_decl = 2;
  1169.       if (prefix_blankline_requested
  1170.           && (parser_state_tos->block_init != 0
  1171.           || parser_state_tos->block_init_level != -1
  1172.           || parser_state_tos->last_token != rbrace
  1173.           || e_code != s_code
  1174.           || e_lab != s_lab
  1175.           || e_com != s_com))
  1176.         prefix_blankline_requested = 0;
  1177.       i = token_end - token + 1;    /* get length of token plus 1 */
  1178.  
  1179.       /* dec_ind = e_code - s_code + (parser_state_tos->decl_indent>i ?
  1180.          parser_state_tos->decl_indent : i); */
  1181.       dec_ind = decl_indent > 0 ? decl_indent : i;
  1182.       goto copy_id;
  1183.  
  1184.     case ident:        /* got an identifier or constant */
  1185.       /* If we are in a declaration, we must indent identifier. But not
  1186.          inside the parentheses of an ANSI function declaration.  */
  1187.       if (parser_state_tos->in_decl
  1188.           && parser_state_tos->p_l_follow == 0
  1189.           && parser_state_tos->last_token != rbrace)
  1190.         {
  1191.           if (parser_state_tos->want_blank)
  1192.         *e_code++ = ' ';
  1193.           parser_state_tos->want_blank = false;
  1194.           if (is_procname == 0 || !procnames_start_line)
  1195.         {
  1196.           if (!parser_state_tos->block_init)
  1197.             if (troff && !parser_state_tos->dumped_decl_indent)
  1198.               {
  1199.             sprintf (e_code, "\n.De %dp+\200p\n",
  1200.                  (int) (dec_ind * 7));
  1201.             parser_state_tos->dumped_decl_indent = 1;
  1202.             e_code += strlen (e_code);
  1203.               }
  1204.             else
  1205.               while ((e_code - s_code) < dec_ind)
  1206.             {
  1207.               CHECK_CODE_SIZE;
  1208.               *e_code++ = ' ';
  1209.             }
  1210.         }
  1211.           else
  1212.         {
  1213.           if (dec_ind && s_code != e_code)
  1214.             dump_line ();
  1215.           dec_ind = 0;
  1216.           parser_state_tos->want_blank = false;
  1217.         }
  1218.         }
  1219.       else if (sp_sw && parser_state_tos->p_l_follow == 0)
  1220.         {
  1221.           sp_sw = false;
  1222.           force_nl = true;
  1223.           parser_state_tos->last_u_d = true;
  1224.           parser_state_tos->in_stmt = false;
  1225.           parse (hd_type);
  1226.         }
  1227.     copy_id:
  1228.       if (parser_state_tos->want_blank)
  1229.         *e_code++ = ' ';
  1230.       if (troff && parser_state_tos->its_a_keyword)
  1231.         {
  1232.           e_code = chfont (&bodyf, &keywordf, e_code);
  1233.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1234.         {
  1235.           CHECK_CODE_SIZE;
  1236.           *e_code++ = keywordf.allcaps && islower (*t_ptr)
  1237.             ? toupper (*t_ptr) : *t_ptr;
  1238.         }
  1239.           e_code = chfont (&keywordf, &bodyf, e_code);
  1240.         }
  1241.       else
  1242.         {
  1243.           /* Troff mode requires that strings be processed specially.  */
  1244.           if (troff && (*token == '"' || *token == '\''))
  1245.         {
  1246.           char qchar;
  1247.  
  1248.           qchar = *token;
  1249.           *e_code++ = '`';
  1250.           if (qchar == '"')
  1251.             *e_code++ = '`';
  1252.           e_code = chfont (&bodyf, &stringf, e_code);
  1253.  
  1254.           t_ptr = token + 1;
  1255.           while (t_ptr < token_end)
  1256.             {
  1257.               *e_code = *t_ptr++;
  1258.               if (*e_code == '\\')
  1259.             {
  1260.               *++e_code = '\\';
  1261.               if (*t_ptr == '\\')
  1262.                 *++e_code = '\\';
  1263.               /* Copy char after backslash.  */
  1264.               *++e_code = *t_ptr++;
  1265.               /* Point after the last char we copied.  */
  1266.               e_code++;
  1267.             }
  1268.             }
  1269.           e_code = chfont (&stringf, &bodyf, e_code - 1);
  1270.           if (qchar == '"')
  1271.             *e_code++ = '\'';
  1272.         }
  1273.           else
  1274.         for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1275.           {
  1276.             CHECK_CODE_SIZE;
  1277.             *e_code++ = *t_ptr;
  1278.           }
  1279.         }
  1280.       parser_state_tos->want_blank = true;
  1281.  
  1282.       /* If the token is va_dcl, it appears without a semicolon, so we
  1283.          need to pretend that one was there.  */
  1284.       if ((token_end - token) == 6
  1285.           && strncmp (token, "va_dcl", 6) == 0)
  1286.         {
  1287.           parser_state_tos->in_or_st = false;
  1288.           parser_state_tos->just_saw_decl--;
  1289.           parser_state_tos->in_decl = 0;
  1290.           parse (semicolon);
  1291.           force_nl = true;
  1292.         }
  1293.       break;
  1294.  
  1295.     case period:        /* treat a period kind of like a binary
  1296.                    operation */
  1297.       *e_code++ = '.';    /* move the period into line */
  1298.       parser_state_tos->want_blank = false;    /* dont put a blank after a
  1299.                            period */
  1300.       break;
  1301.  
  1302.     case comma:
  1303.       /* only put blank after comma if comma does not start the line */
  1304.       parser_state_tos->want_blank = (s_code != e_code);
  1305.       if (parser_state_tos->paren_depth == 0
  1306.           && parser_state_tos->in_decl
  1307.           && is_procname == 0
  1308.           && !parser_state_tos->block_init)
  1309.         while ((e_code - s_code) < (dec_ind - 1))
  1310.           {
  1311.         CHECK_CODE_SIZE;
  1312.         *e_code++ = ' ';
  1313.           }
  1314.  
  1315.       *e_code++ = ',';
  1316.       if (parser_state_tos->p_l_follow == 0)
  1317.         {
  1318.           if (parser_state_tos->block_init_level <= 0)
  1319.         parser_state_tos->block_init = 0;
  1320.           /* If we are in a declaration, and either the user wants all
  1321.              comma'd declarations broken, or the line is getting too
  1322.              long, break the line.  */
  1323.           if (break_comma &&
  1324.           (!leave_comma
  1325.            || (compute_code_target () + (e_code - s_code)
  1326.                > max_col - tabsize)))
  1327.         force_nl = true;
  1328.         }
  1329.       break;
  1330.  
  1331.     case preesc:        /* got the character '#' */
  1332.       if ((s_com != e_com) ||
  1333.           (s_lab != e_lab) ||
  1334.           (s_code != e_code))
  1335.         dump_line ();
  1336.       {
  1337.         int in_comment = 0;
  1338.         int in_cplus_comment = 0;
  1339.         int com_start = 0;
  1340.         char quote = 0;
  1341.         int com_end = 0;
  1342.  
  1343.         /* ANSI allows spaces between '#' and preprocessor directives.
  1344.            Remove such spaces unless user has specified "-lps", in
  1345.            which case also leave any space preceeding the '#'. */
  1346.         if (leave_preproc_space)
  1347.           {
  1348.         char *p = cur_line;
  1349.         while (p < buf_ptr)
  1350.           *e_lab++ = *p++;
  1351.  
  1352.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1353.           {
  1354.             *e_lab++ = *buf_ptr++;
  1355.             if (buf_ptr >= buf_end)
  1356.               fill_buffer ();
  1357.           }
  1358.           }
  1359.         else
  1360.           {
  1361.         *e_lab++ = '#';
  1362.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1363.           if (++buf_ptr >= buf_end)
  1364.             fill_buffer ();
  1365.           }
  1366.  
  1367.         while (*buf_ptr != EOL || (in_comment && !had_eof))
  1368.           {
  1369.         CHECK_LAB_SIZE;
  1370.         *e_lab = *buf_ptr++;
  1371.         if (buf_ptr >= buf_end)
  1372.           fill_buffer ();
  1373.         switch (*e_lab++)
  1374.           {
  1375.           case BACKSLASH:
  1376.             if (troff)
  1377.               *e_lab++ = BACKSLASH;
  1378.             if (!in_comment && !in_cplus_comment)
  1379.               {
  1380.             *e_lab++ = *buf_ptr++;
  1381.             if (buf_ptr >= buf_end)
  1382.               fill_buffer ();
  1383.               }
  1384.             break;
  1385.           case '/':
  1386.             if ((*buf_ptr == '*' || *buf_ptr == '/')
  1387.             && !in_comment && !in_cplus_comment && !quote)
  1388.               {
  1389.             if (*buf_ptr == '/')
  1390.               in_cplus_comment = 1;
  1391.             else
  1392.               in_comment = 1;
  1393.             *e_lab++ = *buf_ptr++;
  1394.             com_start = e_lab - s_lab - 2;
  1395.               }
  1396.             break;
  1397.  
  1398.           case '"':
  1399.           case '\'':
  1400.             if (!quote)
  1401.               quote = e_lab[-1];
  1402.             else
  1403.               if (e_lab[-1] == quote)
  1404.             quote = 0;
  1405.             break;
  1406.  
  1407.           case '*':
  1408.             if (*buf_ptr == '/' && in_comment)
  1409.               {
  1410.             in_comment = 0;
  1411.             *e_lab++ = *buf_ptr++;
  1412.             com_end = e_lab - s_lab;
  1413.               }
  1414.             break;
  1415.           }
  1416.           }
  1417.  
  1418.         while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1419.           e_lab--;
  1420.  
  1421.         if (in_cplus_comment)
  1422.           {
  1423.         in_cplus_comment = 0;
  1424.         *e_lab++ = *buf_ptr++;
  1425.         com_end = e_lab - s_lab;
  1426.           }
  1427.  
  1428.         if (e_lab - s_lab == com_end && bp_save == 0)
  1429.           {            /* comment on preprocessor line */
  1430.         if (save_com.end != save_com.ptr)
  1431.           {
  1432.             need_chars (save_com, 2);
  1433.             *save_com.end++ = EOL;    /* add newline between
  1434.                            comments */
  1435.             *save_com.end++ = ' ';
  1436.             --line_no;
  1437.           }
  1438.         need_chars (save_com, com_end - com_start);
  1439.         strncpy (save_com.end, s_lab + com_start,
  1440.              com_end - com_start);
  1441.         save_com.end += com_end - com_start;
  1442.  
  1443.         e_lab = s_lab + com_start;
  1444.         while (e_lab > s_lab
  1445.                && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1446.           e_lab--;
  1447.         bp_save = buf_ptr;    /* save current input buffer */
  1448.         be_save = buf_end;
  1449.         buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  1450.                        lexi will take tokens out of
  1451.                        save_com */
  1452.         need_chars (save_com, 1);
  1453.         *save_com.end++ = ' ';    /* add trailing blank, just in case */
  1454.         buf_end = save_com.end;
  1455.         save_com.end = save_com.ptr;    /* make save_com empty */
  1456.           }
  1457.         *e_lab = '\0';    /* null terminate line */
  1458.         parser_state_tos->pcase = false;
  1459.       }
  1460.  
  1461.       if (strncmp (s_lab + 1, "if", 2) == 0)
  1462.         {
  1463.           if (blanklines_around_conditional_compilation)
  1464.         {
  1465.           register c;
  1466.           prefix_blankline_requested++;
  1467.           while ((c = *in_prog_pos++) == EOL);
  1468.           in_prog_pos--;
  1469.         }
  1470.           {
  1471.         /* Push a copy of the parser_state onto the stack. All
  1472.            manipulations will use the copy at the top of stack, and
  1473.            then we can return to the previous state by popping the
  1474.            stack.  */
  1475.         struct parser_state *new;
  1476.  
  1477.         new = (struct parser_state *)
  1478.           xmalloc (sizeof (struct parser_state));
  1479.         memcpy (new, parser_state_tos, sizeof (struct parser_state));
  1480.  
  1481.         /* We need to copy the dynamically allocated arrays in the
  1482.            struct parser_state too.  */
  1483.         new->p_stack = (enum codes *)
  1484.           xmalloc (parser_state_tos->p_stack_size
  1485.                * sizeof (enum codes));
  1486.         memcpy (new->p_stack, parser_state_tos->p_stack,
  1487.               parser_state_tos->p_stack_size * sizeof (enum codes));
  1488.  
  1489.         new->il = (int *)
  1490.           xmalloc (parser_state_tos->p_stack_size * sizeof (int));
  1491.         memcpy (new->il, parser_state_tos->il,
  1492.             parser_state_tos->p_stack_size * sizeof (int));
  1493.  
  1494.         new->cstk = (int *)
  1495.           xmalloc (parser_state_tos->p_stack_size
  1496.                * sizeof (int));
  1497.         memcpy (new->cstk, parser_state_tos->cstk,
  1498.             parser_state_tos->p_stack_size * sizeof (int));
  1499.  
  1500.         new->paren_indents = (short *) xmalloc
  1501.           (parser_state_tos->paren_indents_size * sizeof (short));
  1502.         memcpy (new->paren_indents, parser_state_tos->paren_indents,
  1503.              parser_state_tos->paren_indents_size * sizeof (short));
  1504.  
  1505.         new->next = parser_state_tos;
  1506.         parser_state_tos = new;
  1507.           }
  1508.         }
  1509.       else if (strncmp (s_lab + 1, "else", 4) == 0)
  1510.         {
  1511.           /* When we get #else, we want to restore the parser state to
  1512.              what it was before the matching #if, so that things get
  1513.              lined up with the code before the #if.  However, we do not
  1514.              want to pop the stack; we just want to copy the second to
  1515.              top elt of the stack because when we encounter the #endif,
  1516.              it will pop the stack.  */
  1517.           else_or_endif = true;
  1518.           if (parser_state_tos->next)
  1519.         {
  1520.           /* First save the addresses of the arrays for the top of
  1521.              stack.  */
  1522.           enum codes *tos_p_stack = parser_state_tos->p_stack;
  1523.           int *tos_il = parser_state_tos->il;
  1524.           int *tos_cstk = parser_state_tos->cstk;
  1525.           short *tos_paren_indents =
  1526.           parser_state_tos->paren_indents;
  1527.           struct parser_state *second =
  1528.           parser_state_tos->next;
  1529.  
  1530.           memcpy (parser_state_tos, second,
  1531.               sizeof (struct parser_state));
  1532.           parser_state_tos->next = second;
  1533.  
  1534.           /* Now copy the arrays from the second to top of stack to
  1535.              the top of stack.  */
  1536.           /* Since the p_stack, etc. arrays only grow, never shrink,
  1537.              we know that they will be big enough to fit the array
  1538.              from the second to top of stack.  */
  1539.           parser_state_tos->p_stack = tos_p_stack;
  1540.           memcpy (parser_state_tos->p_stack,
  1541.               parser_state_tos->next->p_stack,
  1542.               parser_state_tos->p_stack_size
  1543.               * sizeof (enum codes));
  1544.  
  1545.           parser_state_tos->il = tos_il;
  1546.           memcpy (parser_state_tos->il,
  1547.               parser_state_tos->next->il,
  1548.               parser_state_tos->p_stack_size * sizeof (int));
  1549.  
  1550.           parser_state_tos->cstk = tos_cstk;
  1551.           memcpy (parser_state_tos->cstk,
  1552.               parser_state_tos->next->cstk,
  1553.               parser_state_tos->p_stack_size * sizeof (int));
  1554.  
  1555.           parser_state_tos->paren_indents = tos_paren_indents;
  1556.           memcpy (parser_state_tos->paren_indents,
  1557.               parser_state_tos->next->paren_indents,
  1558.               parser_state_tos->paren_indents_size
  1559.               * sizeof (short));
  1560.         }
  1561.           else
  1562.         diag (1, "Unmatched #else", 0, 0);
  1563.         }
  1564.       else if (strncmp (s_lab + 1, "endif", 5) == 0)
  1565.         {
  1566.           else_or_endif = true;
  1567.           /* We want to remove the second to top elt on the stack, which
  1568.              was put there by #if and was used to restore the stack at
  1569.              the #else (if there was one). We want to leave the top of
  1570.              stack unmolested so that the state which we have been using
  1571.              is unchanged.  */
  1572.           if (parser_state_tos->next)
  1573.         {
  1574.           struct parser_state *second = parser_state_tos->next;
  1575.  
  1576.           parser_state_tos->next = second->next;
  1577.           free (second->p_stack);
  1578.           free (second->il);
  1579.           free (second->cstk);
  1580.           free (second->paren_indents);
  1581.           free (second);
  1582.         }
  1583.           else
  1584.         diag (1, "Unmatched #endif", 0, 0);
  1585.           if (blanklines_around_conditional_compilation)
  1586.         {
  1587.           postfix_blankline_requested++;
  1588.           n_real_blanklines = 0;
  1589.         }
  1590.         }
  1591.  
  1592.       /* Normally, subsequent processing of the newline character
  1593.          causes the line to be printed.  The following clause handles
  1594.          a special case (comma-separated declarations separated
  1595.          by the preprocessor lines) where this doesn't happen. */
  1596.       if (parser_state_tos->last_token == comma
  1597.           && parser_state_tos->p_l_follow <= 0
  1598.           && leave_comma && !parser_state_tos->block_init
  1599.           && break_comma && s_com == e_com)
  1600.         {
  1601.           dump_line ();
  1602.           parser_state_tos->want_blank = false;
  1603.         }
  1604.       break;
  1605.  
  1606.       /* A C or C++ comment. */
  1607.     case comment:
  1608.     case cplus_comment:
  1609.       if (flushed_nl)
  1610.         {
  1611.           flushed_nl = false;
  1612.           dump_line ();
  1613.           parser_state_tos->want_blank = false;
  1614.           force_nl = false;
  1615.         }
  1616.       print_comment ();
  1617.       break;
  1618.     }            /* end of big switch stmt */
  1619.  
  1620.       *e_code = '\0';        /* make sure code section is null terminated */
  1621.       if (type_code != comment
  1622.       && type_code != cplus_comment
  1623.       && type_code != newline
  1624.       && type_code != preesc
  1625.       && type_code != form_feed)
  1626.     parser_state_tos->last_token = type_code;
  1627.  
  1628.     }                /* end of main while (1) loop */
  1629. }
  1630.  
  1631.  
  1632.  
  1633. char *set_profile ();
  1634. void set_defaults ();
  1635. int set_option ();
  1636.  
  1637. /* Points to current input file */
  1638. char *in_name = 0;
  1639.  
  1640. /* Points to the name of the output file */
  1641. char *out_name = 0;
  1642.  
  1643. /* How many input files were specified */
  1644. int input_files;
  1645.  
  1646. /* Names of all input files */
  1647. char **in_file_names;
  1648.  
  1649. /* Initial number of input filenames to allocate. */
  1650. int max_input_files = 128;
  1651.  
  1652.  
  1653. #ifdef DEBUG
  1654. int debug;
  1655. #endif
  1656.  
  1657. main (argc, argv)
  1658.      int argc;
  1659.      char **argv;
  1660. {
  1661.   register int i;
  1662.   struct file_buffer *current_input;
  1663.   char *profile_pathname = 0;
  1664.   int using_stdin = false;
  1665. #ifdef AMIGA
  1666.   int ex_err;
  1667. #endif /* AMIGA */
  1668.  
  1669. #ifdef DEBUG
  1670.   if (debug)
  1671.     debug_init ();
  1672. #endif
  1673.  
  1674. #ifdef AMIGA
  1675.   if (DOSBase->dl_lib.lib_Version < 37)
  1676.     {
  1677.       fprintf (stderr, "You must have Kickstart 2.0 (V37) or higher!\n");
  1678.       exit (20);
  1679.     }
  1680.  
  1681.   if (argc == 2 && stricmp (argv[1],"-h") == 0)
  1682.     {
  1683.       usage ();
  1684.       exit (0);
  1685.     }
  1686.  
  1687.   if ((ex_err = expand_args (&argc, &argv)) == 0)
  1688.     {
  1689.       fprintf (stderr, "Couldn't expand wildcards\n");
  1690.       exit (20);
  1691.     }
  1692.   if (ex_err == 2)
  1693.     {
  1694.       fprintf (stderr, "Wildcards didn't match a file\n");
  1695.       exit (5);
  1696.     }
  1697. #endif /* AMIGA */
  1698.  
  1699.   init_parser ();
  1700.   initialize_backups ();
  1701.  
  1702.   output = 0;
  1703.   input_files = 0;
  1704.   in_file_names = (char **) xmalloc (max_input_files * sizeof (char *));
  1705.  
  1706.   set_defaults ();
  1707.   for (i = 1; i < argc; ++i)
  1708.     if (strcmp (argv[i], "-npro") == 0
  1709.     || strcmp (argv[i], "--ignore-profile") == 0
  1710.     || strcmp (argv[i], "+ignore-profile") == 0)
  1711.       break;
  1712.   if (i >= argc)
  1713.     profile_pathname = set_profile ();
  1714.  
  1715.   for (i = 1; i < argc; ++i)
  1716.     {
  1717.       if (argv[i][0] != '-' && argv[i][0] != '+')    /* Filename */
  1718.     {
  1719.       if (expect_output_file == true)    /* Last arg was "-o" */
  1720.         {
  1721.           if (out_name != 0)
  1722.         {
  1723.           fprintf (stderr, "indent: only one output file (2nd was %s)\n", argv[i]);
  1724.           exit (1);
  1725.         }
  1726.  
  1727.           if (input_files > 1)
  1728.         {
  1729.           fprintf (stderr, "indent: only one input file when output file is specified\n");
  1730.           exit (1);
  1731.         }
  1732.  
  1733.           out_name = argv[i];
  1734.           expect_output_file = false;
  1735.           continue;
  1736.         }
  1737.       else
  1738.         {
  1739.           if (using_stdin)
  1740.         {
  1741.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1742.           exit (1);
  1743.         }
  1744.  
  1745.           input_files++;
  1746.           if (input_files > 1)
  1747.         {
  1748.           if (out_name != 0)
  1749.             {
  1750.               fprintf (stderr, "indent: only one input file when output file is specified\n");
  1751.               exit (1);
  1752.             }
  1753.  
  1754.           if (use_stdout != 0)
  1755.             {
  1756.               fprintf (stderr, "indent: only one input file when stdout is used\n");
  1757.               exit (1);
  1758.             }
  1759.  
  1760.           if (input_files > max_input_files)
  1761.             {
  1762.               max_input_files = 2 * max_input_files;
  1763.               in_file_names
  1764.             = (char **) xrealloc ((char *) in_file_names,
  1765.                           (max_input_files
  1766.                            * sizeof (char *)));
  1767.             }
  1768.         }
  1769.  
  1770.           in_file_names[input_files - 1] = argv[i];
  1771.         }
  1772.     }
  1773.       else
  1774.     {
  1775.       /* '-' as filename means stdin. */
  1776.       if (argv[i][0] == '-' && argv[i][1] == '\0')
  1777.         {
  1778.           if (input_files > 0)
  1779.         {
  1780.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1781.           exit (1);
  1782.         }
  1783.  
  1784.           using_stdin = true;
  1785.         }
  1786.       else
  1787.         i += set_option (argv[i], (i < argc ? argv[i + 1] : 0), 1);
  1788.     }
  1789.     }
  1790.  
  1791.   if (verbose && profile_pathname)
  1792.     fprintf (stderr, "Read profile %s\n", profile_pathname);
  1793.  
  1794.   if (input_files > 1)
  1795.     {
  1796.       /* When multiple input files are specified, make a backup copy
  1797.      and then output the indented code into the same filename. */
  1798.  
  1799.       for (i = 0; input_files; i++, input_files--)
  1800.     {
  1801. #ifdef AMIGA
  1802.           /* Reset line- and comment-count */
  1803.           out_lines = out_coms = 0;
  1804. #endif /* AMIGA */
  1805.       current_input = read_file (in_file_names[i]);
  1806.       in_name = out_name = in_file_names[i];
  1807. #ifdef AMIGA
  1808.           if (verbose)
  1809.             fprintf (stderr, "\n%s:\n", out_name);
  1810. #endif /* AMIGA */
  1811.       output = fopen (out_name, "w");
  1812.       if (output == 0)
  1813.         {
  1814.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1815.           exit (1);
  1816.         }
  1817.  
  1818.       make_backup (current_input);
  1819.       reset_parser ();
  1820.       indent (current_input);
  1821.       if (fclose (output) != 0)
  1822.         sys_error (out_name);
  1823.     }
  1824.     }
  1825.   else
  1826.     {
  1827.       /* One input stream -- specified file, or stdin */
  1828.  
  1829.       if (input_files == 0 || using_stdin)
  1830.     {
  1831.       input_files = 1;
  1832.       in_file_names[0] = "Standard input";
  1833.       current_input = read_stdin ();
  1834.     }
  1835.       else
  1836.     /* 1 input file */
  1837.     {
  1838.       current_input = read_file (in_file_names[0]);
  1839.       if (!out_name && !use_stdout)
  1840.         {
  1841.           out_name = in_file_names[0];
  1842. #ifdef AMIGA
  1843.               if (verbose)
  1844.                 fprintf (stderr, "\n%s:\n", out_name);
  1845. #endif /* AMIGA */
  1846.           make_backup (current_input);
  1847.         }
  1848.     }
  1849.       in_name = in_file_names[0];
  1850.  
  1851.       /* Uset stdout if it was specified ("-st"), or neither input
  1852.          nor output file was specified, or we're doing troff. */
  1853.       if (use_stdout || !out_name || troff)
  1854.     output = stdout;
  1855.       else
  1856.     {
  1857.       output = fopen (out_name, "w");
  1858.       if (output == 0)
  1859.         {
  1860.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1861.           exit (1);
  1862.         }
  1863.     }
  1864.  
  1865.       reset_parser ();
  1866.       indent (current_input);
  1867.     }
  1868.  
  1869.   exit (0);
  1870. }
  1871.  
  1872.  
  1873. #ifdef AMIGA
  1874. char *expand_next_file (pattern)
  1875.      char *pattern;
  1876. {
  1877.   long err;
  1878.   char *pathname;
  1879.   static struct AnchorPath *an = NULL;
  1880.  
  1881.   pathname = NULL;
  1882.   if (pattern == NULL)
  1883.     err = -1;
  1884.   else
  1885.     do
  1886.       {
  1887.         if (an == NULL)
  1888.           {
  1889.             an = malloc (sizeof (struct AnchorPath) + MAXPATH);
  1890.             memset (an, 0, sizeof (struct AnchorPath) + MAXPATH);
  1891.             an->ap_BreakBits = SIGBREAKF_CTRL_C;
  1892.             an->ap_Strlen = MAXPATH;
  1893.             an->ap_Flags = APF_DOWILD;
  1894.             err = MatchFirst (pattern, an);
  1895.           }
  1896.         else
  1897.           err = MatchNext (an);
  1898.  
  1899.         /* Expand only files */
  1900.         if (an->ap_Info.fib_DirEntryType < 0)
  1901.           pathname = an->ap_Buf;
  1902.       } while (err == 0 && pathname == NULL);
  1903.  
  1904.   if (err)
  1905.     {
  1906.       MatchEnd (an);
  1907.       free (an);
  1908.       an = NULL;
  1909.       return NULL;
  1910.     }
  1911.   else
  1912.     return pathname;
  1913. }
  1914.  
  1915.  
  1916. int
  1917. in_prev_args (arg, argv, argc)
  1918.      char *arg, **argv;
  1919.      int argc;
  1920. {
  1921.   int i, is_in_args;
  1922.  
  1923.   is_in_args = 0;
  1924.   for (i = 1; i < argc - 1; i++)
  1925.     if (strcmp (arg, argv[i]) == 0)
  1926.       is_in_args = 1;
  1927.   return is_in_args;
  1928. }
  1929.  
  1930.  
  1931. int
  1932. expand_args (oargc, oargv)
  1933.      int *oargc;
  1934.      char ***oargv;
  1935. {
  1936.   int i;
  1937.   char *str, **argv, buf[MAXPATH];
  1938.   int argc, no_match_at_all, num_matches, contains_wildcards;
  1939.  
  1940.   no_match_at_all = 1;
  1941.   contains_wildcards = 0;
  1942.   argc = 0;
  1943.   argv = malloc (MAXARGS * sizeof (char *));
  1944.   if (argv == NULL)
  1945.     return 0;
  1946.  
  1947.   argv[argc++] = (*oargv)[0];
  1948.   for (i = 1; i < *oargc; i++)
  1949.     {
  1950.       if (ParsePattern ((*oargv)[i], buf, MAXPATH))
  1951.         {
  1952.           contains_wildcards = 1;
  1953.           num_matches = 0;
  1954.           while (str = expand_next_file ((*oargv)[i]))
  1955.             if (argc >= MAXARGS)
  1956.               {
  1957.                 expand_next_file (NULL);
  1958.                 return 0;
  1959.               }
  1960.             else
  1961.               {
  1962.                 /* Avoid duplicate entries */
  1963.                 if (!in_prev_args (str, argv, argc))
  1964.                   {
  1965.                     argv[argc++] = strdup (str);
  1966.                     num_matches++;
  1967.                   }
  1968.               }
  1969.           if (num_matches != 0)
  1970.             no_match_at_all = 0;
  1971.         }
  1972.       else
  1973.         if (argc >= MAXARGS)
  1974.           return 0;
  1975.         else
  1976.           {
  1977.             if ((*oargv)[i][0] != '-' && (*oargv)[i][0] != '+')
  1978.               /* Found a file with no wildcards */
  1979.               no_match_at_all = 0;
  1980.             if (!in_prev_args ((*oargv)[i], argv, argc))
  1981.               argv[argc++] = (*oargv)[i];
  1982.           }
  1983.     }
  1984.   *oargc = argc;
  1985.   *oargv = argv;
  1986.   if (no_match_at_all && contains_wildcards)
  1987.     return 2;
  1988.   else
  1989.     return 1;
  1990. }
  1991. #endif /* AMIGA */
  1992.